<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏 -->
			<view :style="{height: statusBarHeight + 'px'}"></view>
			<!-- 导航栏内容 -->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:navBarHeight + 'px',width:windowWidth + 'px'}" @click.stop="openSearch">
				<view v-if="isSearch" class="navbar-content_search-icons" @click="back">
					<uni-icons type="back" size="22" color="#fff" ></uni-icons>
				</view>
				<view v-if="!isSearch" class="navbar-search">
					<!-- 非搜索页显示 -->
					<view  class="navbar-search_icon">
						<uni-icons type="search" size="16" color="#999"></uni-icons>
					</view>
					<view class="navbar-search_text">uniapp、vue</view>
				</view>
				<view v-else="isSearch" class="navbar-search">
				<!-- 搜索页显示 -->
					<input type="text" value="" placeholder="请输入要搜索的内容" class="navbar-search_text" v-model="value" @input="inputChange"/>
				</view>
			</view>
		</view>
		<!-- 撑个高度 -->
		<view :style="{height:statusBarHeight+navBarHeight + 'px'}"></view>
	</view>
</template>

<script>
	export default {
		name: "navbar",
		props:{
			isSearch: {
				type: Boolean,
				default: false
			},
			value:{
				type:[String,Number],
				default:''
			}
		},
		data() {
			return {
				statusBarHeight: 20,
				navBarHeight: 45,
				windowWidth: 375,

			};
		},
		watch:{
			value(newVal){
				this.value = newVal;
			}
		},
		created() {
			// 获取-手机系统信息
			const info = uni.getSystemInfoSync();
			// 设置-状态栏高度
			this.statusBarHeight = info.statusBarHeight;
			this.windowWidth = info.windowWidth;

			// h5 app mp-alipay
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取-微信小程序-胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏的高度)
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info
				.statusBarHeight);
			this.windowWidth = menuButtonInfo.left;
			// #endif
		},
		methods:{
			openSearch(){
				if(this.isSearch) return;
				console.log('openSearch');
				uni.navigateTo({
					url:"/pages/home-search/home-search"
				});
			},
			inputChange(e){
				const {value} = e.detail;
				// 发送事件
				this.$emit('inputSearch',value);
			},
			back(){
				// 回到首页
				// uni.navigateBack()
				uni.switchTab({
					url:'/pages/tabbar/index/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/css/icons.css';

	.navbar {
		.navbar-fixed {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			width: 100%;
			background-color: $mk-base-color;

			.navbar-content {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 45px;
				padding: 0 15px;
				box-sizing: border-box;

				.navbar-search {
					display: flex;
					align-items: center;
					width: 100%;
					height: 30px;
					padding: 0 10px;
					border-radius: 30px;
					background-color: #fff;

					.navbar-search_icon {
						// width: 10px;
						// height: 10px;
						border: 1px red solid;
						margin-right: 10px;
					}

					.navbar-search_text {
						width: 100%;
						font-size: 14px;
						color: #999;
					}
				}
				&.search{
					padding-left: 0;
					.navbar-content_search-icons{
						margin-left: 10px;
						margin-right: 10px;
					}
					.navbar-search{ 
						border-radius: 5px;
						
					}
				}
			}
			
		}

	}
</style>
